package file1;

import java.io.*;
import java.util.Scanner;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/4/11
 * \* Time: 12:20
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class Demo2 {
    public static void main(String[] args) {
        //1.输入两个路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要拷贝的源路径：");
        String src = scanner.next();
        System.out.println("请输入要拷贝的目标路径：");
        String dest = scanner.next();
        File srcFile = new File(src);
        if(!srcFile.isFile()){
            System.out.println("输入的路径不正确！");
            return;
        }
        //此处不太需要检查目标文件是否存在，OutptuStream写文件的时候能够自动创建不存在的文件
        //2.读取源文件，拷贝到目标文件中
        try(InputStream inputStream = new FileInputStream(src)) {
            try (OutputStream outputStream = new FileOutputStream(dest)){
                // 把 inputStream 中的数据读出来, 写入到 outputStream 中
                byte[] buffer = new byte[1024];
                while (true){
                    int len = inputStream.read(buffer);
                    if(len == -1){
                        //读取完毕
                        break;
                    }
                    outputStream.write(buffer,0,len);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
